﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TimeSyncUtil
{
    public static class Util
    {
        public static void BindNotifyIcon(this Form form, NotifyIcon icon, bool showTaskBar = true)
        {
            form.ShowInTaskbar = showTaskBar;
            form.Resize += (sender, e) =>
            {
                if (form.WindowState == FormWindowState.Minimized)
                {
                    form.Hide();
                    icon.ShowBalloonTip(1000);
                }
            };
            icon.MouseDoubleClick += (sender, e) =>
            {
                if (form.WindowState == FormWindowState.Minimized)
                {
                    form.Show();
                    form.WindowState = FormWindowState.Normal;
                    form.ShowInTaskbar = showTaskBar;
                }
                form.Activate();
            };

            // 创建上下文菜单
            ContextMenu contextMenu = new ContextMenu();
            icon.ContextMenu = contextMenu;
            MenuItem exitMenuItem = new MenuItem("退出");
            contextMenu.MenuItems.Add(exitMenuItem);
            exitMenuItem.Click += (sender, e) =>
            {
                // 退出程序
                icon.Visible = false; // 隐藏托盘图标
                form.Close();
            };
        }
        public static void WarnClose(this Form form)
        {
            form.FormClosing += (sender, e) =>
            {
                if (e.CloseReason == CloseReason.UserClosing)
                {
                    // 显示确认对话框
                    DialogResult result = MessageBox.Show($"确定要退出 {form.Text} 吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (result == DialogResult.Yes)
                    {

                    }
                    else
                    {
                        e.Cancel = true;
                    }
                }
            };
        }
    }
}
